package handlers

import (
	"cargo-mgmt-sys/cache"
	"cargo-mgmt-sys/models"
	"encoding/json"
	"github.com/gin-gonic/gin"
	"strconv"
	"time"
)

func WisdomAdd(c *gin.Context) {
	wis_code := c.PostForm("wis_code")
	wis_name := c.PostForm("wis_name")
	wis_type := c.PostForm("wis_type")
	if wis_code == "" || wis_name == "" || wis_type == "" {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "字段不可为空",
		})
		return
	}
	data := models.Wisdom{
		WisCode: wis_code,
		WisName: wis_name,
		WisType: wis_type,
	}
	err := models.DB.Create(&data).Error
	if err != nil {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "添加失败",
		})
		return
	}
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "添加成功",
	})
	return
}

func WisdomOrderAdd(c *gin.Context) {
	order_code := c.PostForm("order_code")
	order_provide := c.PostForm("order_provide")
	order_status := c.PostForm("order_status")
	wid, _ := strconv.Atoi(c.PostForm("wid"))
	if order_code == "" || order_provide == "" || order_status == "" || wid == 0 {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "字段不可为空",
		})
		return
	}
	data := models.WisOrder{
		OrderCode:    order_code,
		OrderProvide: order_provide,
		OrderStatus:  order_status,
		Wid:          wid,
	}
	err := models.DB.Create(&data).Error
	if err != nil {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "添加失败",
		})
		return
	}
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "添加成功",
	})
	return
}

func WisdomOrderList(c *gin.Context) {
	page, _ := strconv.Atoi(c.DefaultQuery("page", "1"))
	size, _ := strconv.Atoi(c.DefaultQuery("size", "10"))
	offset := (page - 1) * size

	redis_key := "wis_order_list" + strconv.Itoa(page)
	var list []models.WisOrderList
	/*
		 SELECT
		  wis_orders.id as OrderId,
		  wisdoms.wis_code as WisCode,
		  wisdoms.wis_name as WisName,
		  wisdoms.wis_type as WisType,
		  wis_orders.order_code as OrderCode,
		  wis_orders.order_provide as OrderProvide,
		  wis_orders.order_status as OrderStatus
		FROM
		  wisdoms
		  LEFT JOIN wis_orders ON wis_orders.wid = wisdoms.id
		WHERE
		  wis_orders.id = 1
	*/
	cacheData, _ := cache.Rdb.Get(cache.Ctx, redis_key).Result()
	if cacheData == "" {
		models.DB.Table("wisdoms").
			Select("wis_orders.id as OrderId",
				"wisdoms.wis_code as WisCode",
				"wisdoms.wis_name as WisName",
				"wisdoms.wis_type as WisType",
				"wis_orders.order_code as OrderCode",
				"wis_orders.order_provide as OrderProvide",
				"wis_orders.order_status as OrderStatus",
			).
			Joins("LEFT JOIN wis_orders ON wis_orders.wid = wisdoms.id").
			Offset(offset).
			Limit(size).
			Find(&list)
		jsonData, _ := json.Marshal(list)
		cache.Rdb.Set(cache.Ctx, redis_key, jsonData, time.Hour*2)
	} else {
		json.Unmarshal([]byte(cacheData), &list)
	}

	var count int64
	models.DB.Model(&models.WisOrder{}).Count(&count)

	c.JSON(200, gin.H{
		"code":  200,
		"msg":   "ok",
		"data":  list,
		"count": count,
	})
	return
}

func WisdomOrderDetail(c *gin.Context) {
	id := c.Query("id")

	redis_key := "wis_order_detail" + id
	var list models.WisOrderList
	/*
		 SELECT
		  wis_orders.id as OrderId,
		  wisdoms.wis_code as WisCode,
		  wisdoms.wis_name as WisName,
		  wisdoms.wis_type as WisType,
		  wis_orders.order_code as OrderCode,
		  wis_orders.order_provide as OrderProvide,
		  wis_orders.order_status as OrderStatus
		FROM
		  wisdoms
		  LEFT JOIN wis_orders ON wis_orders.wid = wisdoms.id
		WHERE
		  wis_orders.id = 1
	*/
	cacheData, _ := cache.Rdb.Get(cache.Ctx, redis_key).Result()
	if cacheData == "" {
		models.DB.Table("wisdoms").
			Select("wis_orders.id as OrderId",
				"wisdoms.wis_code as WisCode",
				"wisdoms.wis_name as WisName",
				"wisdoms.wis_type as WisType",
				"wis_orders.order_code as OrderCode",
				"wis_orders.order_provide as OrderProvide",
				"wis_orders.order_status as OrderStatus",
			).
			Joins("LEFT JOIN wis_orders ON wis_orders.wid = wisdoms.id").
			Where("wis_orders.id = ?", id).
			Find(&list)
		jsonData, _ := json.Marshal(list)
		cache.Rdb.Set(cache.Ctx, redis_key, jsonData, time.Hour*2)
	} else {
		json.Unmarshal([]byte(cacheData), &list)
	}

	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "ok",
		"data": list,
	})
	return
}
